package com.fs.jzzn.repository;

import java.util.Collection;
import java.util.List;

import org.activiti.engine.history.HistoricProcessInstance;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import com.fs.jzzn.pojo.Project;
/**
 * @author TiddlerCJ
 */
public interface ProjectRepository extends JpaRepository<Project,String>, JpaSpecificationExecutor<Project>{
	/**
	 * 解除合同绑定关系
	 */
	@Transactional
	@Modifying
	@Query(value = "update p_project p set p.contract_no = '' where p.id = ?1",nativeQuery = true)
	void unship(String id);
	
	@Transactional
	@Modifying
	@Query(value = "update p_project p set p.contract_no =  ?1 where p.id = ?2",nativeQuery = true)
	void ship(String contractNo,String ids);
	
	/**
	 * 通过主键查询
	 * @param ids
	 * @return
	 */
	List<Project> findByIdIn(Collection<String> ids);
	
	Project findOneBypNo(String pNo);
	
	@Query(value = "SELECT fs.modle_id as pId,p.p_road as pRoad,fs.node_name as nodeName,fs.create_time as createTime,p.p_no as pNo FROM p_flow_status fs  LEFT JOIN p_project p on p.id = fs.modle_id WHERE fs.node_user_id <> '' AND fs.node_user_id LIKE CONCAT('%',:userId,'%')  AND p.p_no LIKE CONCAT('%',:pNo,'%')  ORDER BY fs.create_time desc ",nativeQuery = true)
	List<Object> getTasks(@Param("userId")String userId,@Param("pNo")String pNo);
	//List<Object> getTasks(@Param("userId")String userId);
	
	@Query(value = "SELECT * FROM act_hi_procinst WHERE PROC_INST_ID_ IN ( SELECT PROC_INST_ID_ FROM act_hi_taskinst WHERE ASSIGNEE_ = ?1)   ORDER BY START_TIME_ DESC ",nativeQuery = true)
	List<HistoricProcessInstance> findProcessInstanceByUserId(String userId); 
}
